package com.ug.managent.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ug.common.core.domain.entity.SysUser;
import com.ug.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ug.common.annotation.Log;
import com.ug.common.core.controller.BaseController;
import com.ug.common.core.domain.AjaxResult;
import com.ug.common.enums.BusinessType;

import com.ug.managent.domain.WorkOrder;
import com.ug.managent.service.IWorkOrderService;
import com.ug.common.utils.poi.ExcelUtil;
import com.ug.common.core.page.TableDataInfo;

/**
 * 工单主Controller
 * 
 * @author Leo
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/managent/order")
public class WorkOrderController extends BaseController
{
    @Autowired
    private IWorkOrderService workOrderService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询工单主列表
     */
    @PreAuthorize("@ss.hasPermi('managent:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrder workOrder)
    {
        startPage();
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('managent:order:worker')")
    @GetMapping("/worker")
    public AjaxResult worker()
    {
        List<SysUser> sysUserList = sysUserService.selectWorkerList(101L);
        return success(sysUserList);
    }

    @PreAuthorize("@ss.hasPermi('managent:order:worker')")
    @GetMapping("/workerOrder")
    public AjaxResult workOrder(Long workerId)
    {
        List<WorkOrder> list = workOrderService.selectWorkerOrderList(workerId, 1L);
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('managent:order:worker')")
    @GetMapping("/workerRepair")
    public AjaxResult workRepair(Long workerId)
    {
        List<WorkOrder> list = workOrderService.selectWorkerOrderList(workerId, 2L);
        return success(list);
    }


    /**
     * 导出工单主列表
     */
    @PreAuthorize("@ss.hasPermi('managent:order:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrder workOrder)
    {
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        ExcelUtil<WorkOrder> util = new ExcelUtil<WorkOrder>(WorkOrder.class);
        util.exportExcel(response, list, "工单主数据");
    }

    /**
     * 获取工单主详细信息
     */
    @PreAuthorize("@ss.hasPermi('managent:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(workOrderService.selectWorkOrderById(id));
    }

    /**
     * 新增工单主
     */
    @PreAuthorize("@ss.hasPermi('managent:order:add')")
    @Log(title = "工单主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrder workOrder)
    {
        return toAjax(workOrderService.insertWorkOrder(workOrder));
    }

    /**
     * 修改工单主
     */
    @PreAuthorize("@ss.hasPermi('managent:order:edit')")
    @Log(title = "工单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrder workOrder)
    {
        return toAjax(workOrderService.updateWorkOrder(workOrder));
    }

    /**
     * 删除工单主
     */
    @PreAuthorize("@ss.hasPermi('managent:order:remove')")
    @Log(title = "工单主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(workOrderService.deleteWorkOrderByIds(ids));
    }
}
